package site.lisl.mynote.algorithm.queue;

/**
 * @author Administrator
 */
public class CycleArrayQueue {

    private Object[] items;
    private Integer head;
    private Integer tail;

    private Integer size;


    public CycleArrayQueue(int capacity) {
        this.items = new Object[capacity];
        this.head = 0;
        this.tail = 0;
        this.size = capacity;
    }

    /**
     * 入队
     * @param obj
     * @return
     */
    public boolean enQueue(Object obj) {
        if((tail + 1) % size == head) {
            return false;
        }

        items[tail] = obj;
        tail = (tail + 1) % size;
        return true;
    }

    public Object deQueue(){
        if(head.equals(tail)){
            //表示空队列
            return null;
        }

        Object item = items[head];
        head = (head + 1) % size;
        return item;
    }
}
